package com.app.demo.activitys;

import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

import com.app.demo.R;
import com.app.shop.mylibrary.base.BaseActivity;
import com.app.shop.mylibrary.beans.UserBean;
import com.app.shop.mylibrary.utils.StringUtil;
import com.app.shop.mylibrary.utils.ToastUtil;
import com.app.shop.mylibrary.utils.UserManager;

import com.app.demo.activitys.LoginActivity;
import com.app.demo.activitys.ModifyActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RegisterActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.edtName)
    EditText edtName;
    @BindView(R.id.edtMobile)
    EditText edtMobile;
    @BindView(R.id.edtpwd)
    EditText edtpwd;
    @BindView(R.id.edtpwd_repeat)
    EditText edtpwdRepeat;
    @BindView(R.id.register_TxVerifCode)
    TextView VerifCode;
    @BindView(R.id.register_inputVerifCode)
    EditText userVerifCode;
    @BindView(R.id.edtStuNum)
    EditText edtstuNum;
    private int type;

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
        type = getIntent().getIntExtra("type", 0);
        tvTitle.setText("注册");
        VerifCode.setText(LoginActivity.genVerifCode());
    }

    @OnClick({R.id.imgv_return, R.id.tv_register,R.id.register_TxVerifCode})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.imgv_return:
                onBackPressed();
                break;
            case R.id.register_TxVerifCode:
                VerifCode.setText(LoginActivity.genVerifCode());
                break;
            case R.id.tv_register:

                if (StringUtil.isEmpty(edtName.getText().toString())) {
                    ToastUtil.showToast(this, "请输入昵称");
                    return;
                }

                if (StringUtil.isEmpty(edtstuNum.getText().toString())) {
                    ToastUtil.showToast(this, "请输入学号");
                    return;
                }

                if (StringUtil.isEmpty(edtMobile.getText().toString())) {
                    ToastUtil.showToast(this, "请输入手机号");
                    return;
                }

                if (StringUtil.isEmpty(edtpwd.getText().toString())) {
                    ToastUtil.showToast(this, "请输入密码");
                    return;
                }

                if (StringUtil.isEmpty(edtpwdRepeat.getText().toString())) {
                    ToastUtil.showToast(this, "请再次输入密码");
                    return;
                }

                if(StringUtil.isEmpty(userVerifCode.getText().toString())){
                    ToastUtil.showToast(this, "请输入验证码");
                    return;
                }

                if(!checkStudentNum(edtstuNum.getText().toString())){
                    ToastUtil.showToast(this, "学号不合法");
                    return;
                }
                if(!ModifyActivity.checkMobileNum(edtMobile.getText().toString())){
                    ToastUtil.showToast(this, "手机号不合法");
                    return;
                }
                if(!LoginActivity.checkStringEqual(userVerifCode.getText().toString(),VerifCode.getText().toString())){
                    ToastUtil.showToast(this, "验证码错误");
                    VerifCode.setText(LoginActivity.genVerifCode());
                    return;
                }
                if (!edtpwd.getText().toString().equals(edtpwdRepeat.getText().toString())) {
                    ToastUtil.showToast(this, "两次密码不一致");
                    return;
                }

                if(checkPassword(edtpwd.getText().toString())==1){
                    ToastUtil.showToast(this, "密码太短");
                    return;
                }
                if(checkPassword(edtpwd.getText().toString())==2){
                    ToastUtil.showToast(this, "密码太长");
                    return;
                }
                if (UserManager.isHaveUser(edtstuNum.getText().toString())) {
                    ToastUtil.showToast(this, "已存在该用户");
                    return;
                }

                UserBean userBean = new UserBean();
                userBean.setUser_id(edtstuNum.getText().toString());
                userBean.setName(edtName.getText().toString());
                userBean.setMobile(edtMobile.getText().toString());
                userBean.setPassword(edtpwd.getText().toString());
                userBean.setType(type);
                userBean.save();

                ToastUtil.showToast(this, "注册成功");

                onBackPressed();

                break;
        }
    }

    //返回值为1,密码太短
    //返回值为2,密码太长
    //返回值为0,密码合法
    public static int checkPassword(String str){
        int result;
        if(str.length()<6) result=1;
        else if(str.length()>16) result=2;
        else result=0;
        return result;
    }

    //返回值为true,学号合法
    //返回值为false,学号不合法
    public static boolean checkStudentNum(String str){
        if(str.length()!=11) return false;
        for(int i=0;i<str.length();++i){
            if(!Character.isDigit(str.charAt(i))) return false;
        }
        return true;
    }
}
